package br.com.emmis.tercons.model;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.util.Patterns;

import java.util.Locale;
import java.util.regex.Pattern;

import br.com.emmis.tercons.constants.Constants;

/**
 * Created by vaner on 27/02/2015.
 */
public class Config
{
    private Context mContext;
    private static boolean LOADED = false;

    private boolean mEnableSound;
    private boolean mKeepScreenOn;
    private boolean mAutoPray;
    private boolean mNotifyIntentions;
    private boolean mPrayReminder;
    private boolean mRemoveAds;
    private boolean mDonor;
    private String mLocation;
    private String mApp;
    private String mIntentionURL;
    private String mUserEmail;
	private String mAppVersion;

    private static class ConfigHolder {
        public static Config mConfig = new Config();
    }

    public static Config getInstance(Context ctx) {
        ConfigHolder.mConfig.setmContext(ctx);
        if(!LOADED) {
            ConfigHolder.mConfig.loadConfig();
        }
        return ConfigHolder.mConfig;
    }

    private void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    private void loadConfig() {
        LOADED = true;

        // Restore preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mEnableSound = settings.getBoolean(Constants.PREF_ENABLE_SOUND, false);
        mKeepScreenOn = settings.getBoolean(Constants.PREF_KEEP_SCREEN_ON, false);
        mAutoPray  = settings.getBoolean(Constants.PREF_AUTO_PRAY, false);
        mNotifyIntentions  = settings.getBoolean(Constants.PREF_NOTIFY_INTENTIONS, false);
        mPrayReminder  = settings.getBoolean(Constants.PREF_NOTIFY_REMINDER, false);
        mRemoveAds  = settings.getBoolean(Constants.PREF_REMOVE_ADS, false);
        mDonor = settings.getBoolean(Constants.PREF_DONATOR, false);

        mLocation = Locale.getDefault().getLanguage();
        mApp = Constants.APP_ID;
        mIntentionURL = settings.getString(Constants.PREF_INTENTION_URL, Constants.INTENTIONS_URL);

        Pattern gmailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
        Account[] accounts = AccountManager.get(mContext).getAccounts();
        mUserEmail="";
        for (Account account : accounts) {
            if (gmailPattern.matcher(account.name).matches()) {
                mUserEmail += account.name + ";";
            }
        }

        PackageInfo pInfo = null;
        mAppVersion = "X";
        try {
            pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            mAppVersion = pInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }

    public boolean ismEnableSound() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mEnableSound = settings.getBoolean(Constants.PREF_ENABLE_SOUND, false);
        return mEnableSound;
    }

    public boolean ismKeepScreenOn() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mKeepScreenOn = settings.getBoolean(Constants.PREF_KEEP_SCREEN_ON, false);
        return mKeepScreenOn;
    }

    public boolean ismAutoPray() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mAutoPray  = settings.getBoolean(Constants.PREF_AUTO_PRAY, false);
        return mAutoPray;
    }

    public boolean ismNotifyIntentions() {
        // Restore preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mNotifyIntentions  = settings.getBoolean(Constants.PREF_NOTIFY_INTENTIONS, false);
        return mNotifyIntentions;
    }

    public boolean ismPrayReminder() {
        // Restore preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mPrayReminder  = settings.getBoolean(Constants.PREF_NOTIFY_REMINDER, false);
        return mPrayReminder;
    }

    public boolean ismRemoveAds() {
        // Restore preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mRemoveAds  = settings.getBoolean(Constants.PREF_REMOVE_ADS, false);
        return mRemoveAds;
    }

    public void setmRemoveAds(boolean mRemoveAds) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(Constants.PREF_REMOVE_ADS, mRemoveAds);
        editor.commit();
        this.mRemoveAds = mRemoveAds;
    }

    public boolean ismDonor() {
        // Restore preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mDonor = settings.getBoolean(Constants.PREF_DONATOR, false);
        return mDonor;
    }

    public void setmDonor(boolean mDonor) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(Constants.PREF_DONATOR, mDonor);
        editor.commit();
        this.mDonor = mDonor;
    }

    public String getmLocation() {
        return mLocation;
    }

    public String getmApp() {
        return mApp;
    }

    public String getmIntentionURL() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        mIntentionURL = settings.getString(Constants.PREF_INTENTION_URL, Constants.INTENTIONS_URL);
        return mIntentionURL;
    }

    public void setmIntentionURL(String mGetIntentionURL) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(Constants.PREF_INTENTION_URL, mGetIntentionURL);
        editor.commit();
        this.mIntentionURL = mGetIntentionURL;
    }

    public String getmUserEmail() {
        return mUserEmail;
    }

    @Override
    public String toString() {
		loadConfig();
        return "{" +
                "Sound=" + mEnableSound +
                ", Screen=" + mKeepScreenOn +
                ", Auto=" + mAutoPray +
                ", Intent=" + mNotifyIntentions +
                ", Remind=" + mPrayReminder +
                ", RemAds=" + mRemoveAds +
                ", Donor=" + mDonor +
				", Ver="  + mAppVersion +
                '}';
    }
}
